package com.sghd.football.module.game.service.match;

import com.sghd.football.module.game.service.GameService;
import com.sghd.football.module.game.service.core.Player;
import com.sghd.football.module.game.service.thread.TaskItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author wch
 * @date 2018-07-03 14:02
 * @desc 匹配器抽象实现
 **/
public abstract class AbstractMatcher implements Matcher{

	/** 等待加入的玩家队列*/
	protected Map<Long, Player> players = new HashMap<>();

	protected abstract void doMatch();
	@Autowired
	protected GameService gameService;
	/** 匹配间隔时间*/
	@Value("${game.matching:3000}")
	private long matching;
	@PostConstruct
	void init(){
		timer(new Date());//开始匹配
	}

	public void timer(Date now) {
		gameService.addTimer(null, (desk, now1) -> {
			match(now1);
		}, new Date(now.getTime() + matching), TaskItem.MATCH);
	}

	@Override
	public void add(Player player) {
		players.put(player.getPlayerId(), player);
	}

	@Override
	public void match(Date now) {
		doMatch();
		timer(now);
	}
}
